import { store } from "@/store";
import { defineStore } from "pinia";
import {
  setAllToken,
  setUserInfo,
  getUserInfo,
  getAllToken,
  deleteUserCache,
  getToken,
  setToken,
  setBind,
  getBind,
} from "@/utils/auth";
export const useUserStore = defineStore("user", {
  state: () => ({
    userInfo: getUserInfo(),
    allToken: getAllToken(),
    token: getToken(),
    isBind: getBind(),
    isShowLogin: false,
  }),
  getters: {
    getUserInfo() {
      return this.userInfo;
    },
    getAllToken() {
      return this.allToken;
    },
    getToken() {
      return this.token;
    },
    getIsBind() {
      return this.isBind;
    },
    getisShowLogin() {
      return this.isShowLogin;
    },
  },
  actions: {
    setUserInfoAction(data) {
      this.userInfo = data;
      setUserInfo(data);
    },
    setAllTokenAction(data) {
      this.allToken = data;
      setAllToken(data);
    },
    setTokenAction(data) {
      this.token = data;
      setToken(data);
    },
    deleteUserCacheAction() {
      this.userInfo = null;
      this.allToken = null;
      this.token = null;
      this.isBind = null;
      deleteUserCache();
    },
    setIsBind(data) {
      this.isBind = data;
      setBind(data);
    },
    setisShowLogin(data) {
      this.isShowLogin = data;
    },
  },
});

export const useUserStoreWithOut = () => {
  return useUserStore(store);
};
